package kliment.flex.spark.controls
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ArrayList;
	import mx.controls.TabBar;
	import mx.core.IVisualElement;
	import mx.events.FlexEvent;
	import mx.styles.StyleProxy;
	
	import spark.components.Group;
	import spark.components.VGroup;
	
	[ResourceBundle("components")]
	
	[DefaultProperty("mxmlContent")] 
	
	public class TabNavigator2 extends Group
	{
		private var _holder:VGroup;
		private var _contentHolder:Group;
		private var _tabBar:TabBar;
		
		private var _dataProvider:ArrayList = new ArrayList([]);
		private var _selectedIndex:int;
		
		public function TabNavigator2()
		{
			super();
			
			_holder = new VGroup();
			_contentHolder = new Group();
			_tabBar = new TabBar();
			_tabBar.focusEnabled = false;
			_tabBar.styleName = new StyleProxy(this, _tabBarStyleFilters);
			_tabBar.addEventListener(FlexEvent.VALUE_COMMIT, _tabBarHnadler);
			
			_tabBar.percentWidth = 100;
			_contentHolder.percentWidth = 100;
			_contentHolder.percentHeight = 100;
			_holder.gap = 0;
		}
		
		override public function addElement(element:IVisualElement):IVisualElement
		{
			return _contentHolder.addElement(element);
		}
		
		override public function addElementAt(element:IVisualElement, index:int):IVisualElement
		{
			element.visible = false;
			_dataProvider.addItemAt(element, index);
			return _contentHolder.addElementAt(element, index);
		}
		
		override public function removeElement(element:IVisualElement):IVisualElement
		{
			return _contentHolder.removeElement(element);
		}
		
		override public function removeElementAt(index:int):IVisualElement
		{
			_dataProvider.removeItemAt(index);
			return _contentHolder.removeElementAt(index);
		}
		
		override public function removeAllElements():void
		{
			_dataProvider.removeAll();
			_contentHolder.removeAllElements();
		}
		
		private function _tabBarHnadler(event:Event):void {
			if (!_tabBar || _tabBar.selectedIndex < 0)
				return;
			var item:IVisualElement = _dataProvider.getItemAt(_tabBar.selectedIndex) as IVisualElement;
				item.visible = true; 
		}
		
		override protected function createChildren():void
		{
			super.createChildren();
			
			_holder.addElement(_tabBar);
			_holder.addElement(_contentHolder);
			super.addElementAt(_holder, 0);
		}
		
		private static var _tabBarStyleFilters:Object =
			{
				"firstTabStyleName" : "firstTabStyleName",
				"horizontalAlign" : "horizontalAlign",
				"horizontalGap" : "horizontalGap",
				"lastTabStyleName" : "lastTabStyleName",
				"selectedTabTextStyleName" : "selectedTabTextStyleName",
				"tabStyleName" : "tabStyleName",
				"tabWidth" : "tabWidth",
				"verticalAlign" : "verticalAlign",
				"verticalGap" : "verticalGap"
			}; 
		
		override public function set mxmlContent(value:Array):void
		{
			_contentHolder.mxmlContent = value;
			_dataProvider.source = value;
			_tabBar.dataProvider = _dataProvider;
		}
		
		public function get dataProvider():ArrayList { return _dataProvider; }
		public function set dataProvider(value:ArrayList):void 
		{
			_dataProvider = value;
			_contentHolder.mxmlContent = _dataProvider.source;
			_tabBar.dataProvider = _dataProvider;
		}
		
		public function get selectedIndex():int { return _selectedIndex; }
		public function set selectedIndex(value:int):void
		{
			if (value == selectedIndex && value != -1)
				return;
			
			_selectedIndex = value;
			_tabBar.selectedIndex = value;
		}
	}
}